// 共享内存定义（8KB消息的Ring Buffer）
struct alignas(64) ShmemRing { // 64字节对齐，避免False Sharing
    std::atomic<uint64_t> head; // 生产者指针
    std::atomic<uint64_t> tail; // 消费者指针
    char buffer[1024][8192];    // 1024个8KB消息槽
};

// 生产者写入
void produce(ShmemRing* ring, const Message* msg) {
    uint64_t head = ring->head.load(std::memory_order_relaxed);
    uint64_t next_head = (head + 1) % 1024;
    while (next_head == ring->tail.load(std::memory_order_acquire)) {
        // 缓冲区满，等待或处理（可自旋或休眠）
    }
    memcpy(ring->buffer[head], msg, msg->size);
    ring->head.store(next_head, std::memory_order_release);
}

// 消费者读取
void consume(ShmemRing* ring, Message* out) {
    uint64_t tail = ring->tail.load(std::memory_order_relaxed);
    while (tail == ring->head.load(std::memory_order_acquire)) {
        // 缓冲区空，等待
    }
    memcpy(out, ring->buffer[tail], out->size);
    ring->tail.store((tail + 1) % 1024, std::memory_order_release);
}